package tom.client;

import java.util.Stack;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.ui.*;

public class MainPanel extends SimplePanel {
	public static MainPanel mainPanel = GWT.create(MainPanel.class);

	public static final int HEIGHT = 500;
	public static final int WIDTH = 900;
	public static final String pHEIGHT = "500";
	public static final String pWIDTH = "900";
	private MainMenu menu;
	private Stack<Panel> st;

	public void reset() {
		this.menu.refresh();
		show(menu);
	}

	public void show(MainMenu menu) {
		this.menu = menu;
		this.setWidget(menu);
		st.clear();
		if (WindMill.clock != null)
			WindMill.clock.resetCount();
	}

	public void show(Panel p, String title) {
		st.push(p);
		this.setWidget(p);
		//p.setTitle(title);
		WindMill.setStatus(title);
		if (WindMill.clock != null)
			WindMill.clock.resetCount();
	}

	public boolean back() {
		boolean canBack = true;
		if (!st.isEmpty()) {
			Panel p = st.pop();
			if (p instanceof DataForm)
				((DataForm) p).clearFields();
		}
		if (!st.isEmpty()) {
			Panel p = st.pop();
			show(p, p.getTitle());
		} else {
			show(this.menu);
		}
		return canBack;
	}

	public MainPanel() {
		this.setPixelSize(WIDTH, HEIGHT);
		this.setStyleName("toolbar");
		st = new Stack<Panel>();
		Event.addNativePreviewHandler(new NativePreviewHandler() {

			public void onPreviewNativeEvent(NativePreviewEvent event) {
				NativeEvent ne = event.getNativeEvent();
				if (ne.getCtrlKey()
						&& (ne.getKeyCode() == 'l' || ne.getKeyCode() == 'L')) {
					ne.preventDefault();
				}
			}
		});

	}

}
